﻿using System.Windows.Input;
using Ninject.Core.Parameters;
using TestBat.IModels.Domain;
using TestBat.IModels.Models;
using TestBat.IViewModels.Workspaces.Tasks;
using TestBat.ServiceLocator;
using TestBat.ViewModels.Attributes;
using TestBat.ViewModels.Base.Commands;

namespace TestBat.ViewModels.Workspaces.Tasks
{
    public class MultipleResponseTaskViewModel : TaskViewModel, IMultipleResponseTaskViewModel
    {
        public MultipleResponseTaskViewModel(IMultipleResponseTask task, ITaskModel taskModel)
            : base(task, taskModel)
        {
            Task = task;
        }


        public IChoiceListViewModel Choices
        {
            get
            {
                if (choices == null)
                    choices = DefaultServiceLocator.Get<IChoiceListViewModel>(With.Parameters.ConstructorArgument("task", Task));

                return choices;
            }
        }

        public ICommand AddCommand
        {
            get
            {
                return new RelayCommand(ctx => Add());
            }
        }

        public override void OnLostFocus()
        {
            base.OnLostFocus();

            choices.RecreateItems();
        }

        public ICommand RemoveCommand
        {
            get
            {
                return new RelayCommand(ctx => Remove(Choices.Selected.Choice), ctx => Choices.Selected != null);
            }
        }

        public new IMultipleResponseTask Task
        {
            get;
            private set;
        }

        [BusinessErrorDialog]
        private void Add()
        {
            // create choice
            taskModel.AddChoice(Task);

            // recreate items
            Choices.RecreateItems();
        }

        [BusinessErrorDialog]
        private void Remove(IChoice choice)
        {
            // create choice
            taskModel.RemoveChoice(choice);

            // recreate items
            Choices.RecreateItems();
        }

        private IChoiceListViewModel choices;
    }
}
